#pragma once

#include "log.hpp"
#include <iostream>
#include <cstring>
#include <cassert>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <fcntl.h>

static const char* g_path_name = ".";    // 项目路径
static const int g_proj_id = 0x66;       // 项目编号(id)

static const size_t g_shm_size = 4096;   // 共享内存大小（一页）

// 通过管道对共享内存添加访问限制

#define MODE 0666
static const char* g_fifo_fikename = "./fifo.ipc";

class Init
{
public:
    Init()
    {
        int n = mkfifo(g_fifo_fikename, MODE);
        assert(n == 0);
        (void)n;
    }

    ~Init()
    {
        int n = unlink(g_fifo_fikename);
        assert(n == 0);
        (void)n;
    }
};

// 管道文件打开函数
int OpenFIFO(const char* filename, int flags)
{
    int fd = open(filename, flags);
    assert(fd >= 0);
    return fd;
}

// 阻塞等待函数
void Wait(int fd)
{
    uint32_t n = 0;
    ssize_t sz = read(fd, &n, sizeof(n));
    assert(n > 0);
    (void)n;
}

// 唤醒对端读取操作函数
void Wake(int fd)
{
    uint32_t msg = 1;
    ssize_t n = write(fd, &msg, sizeof(msg));
    assert(n > 0);
    (void)n;
}

// 关闭管道文件函数
int CloseFIFO(int fd)
{
    close(fd);
}